﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Collections;

using XJX.DBCore;

namespace XJX.DataLayer
{
    public sealed class Listening : XJX.DBCore.DBObject
    {
        public Listening(string newConnectionString) : base(newConnectionString) { }

        public bool LB_Add_New_ListeningBook_Info
            (
            string Title,
            string Description,
            string AnnouncerInfo,
            string FileInfo,
            string TimeLength,
            string BookLinkPic,
            string BookLinkInfo,
            out int ReturnValue,          //返回参数值
            out string ReturnFieldValue   //返回字段值,返回插入的新值ID编号
            )
        {
            int rowsAffected;//受影响的行数．

            SqlParameter[]
                parameters = {
								 new SqlParameter("@title",SqlDbType.VarChar,500), 
								 new SqlParameter("@Description",SqlDbType.VarChar,-1),
								 new SqlParameter("@AnnouncerInfo",SqlDbType.VarChar,5000),
								 new SqlParameter("@FileInfo",SqlDbType.VarChar,1000), 
								 new SqlParameter("@TimeLength",SqlDbType.VarChar,500), 
								 new SqlParameter("@BookLinkPic",SqlDbType.VarChar,1000),
								 new SqlParameter("@BookLinkInfo",SqlDbType.VarChar,5000)
			};

            parameters[0].Value = Title;
            parameters[1].Value = Description;
            parameters[2].Value = AnnouncerInfo;
            parameters[3].Value = FileInfo;
            parameters[4].Value = TimeLength;
            parameters[5].Value = BookLinkPic;
            parameters[6].Value = BookLinkInfo;

            try
            {
                //以下语句可以得到三个值，从而进行流程判断。一个是返回值，一个是影响的行数，一个是返回的指定字段值。
                ReturnValue = RunProcedure("LB_Add_New_ListeningBook", parameters, out rowsAffected, out ReturnFieldValue);
            }
            catch (SqlException e)
            {
                throw new System.Exception(e.Message);
            }
            return (rowsAffected == 1);
        }

        public bool LB_Del_One_ListeningBook_From_LBID(string LBID)
        {
            int rowsAffected;

            SqlParameter[]
                parameters = {
								 new SqlParameter("@LBID",SqlDbType.Int)
			};

            parameters[0].Value = LBID;

            try
            {
                RunProcedure("[LB_Del_One_ListeningBook_From_LBID]", parameters, out rowsAffected);
            }
            catch (SqlException e)
            {
                throw new System.Exception(e.Message);
            }
            return (rowsAffected == 1);
        }

        public bool LB_Modify_One_ListeningBook_Info
            (
            string LBID,
            string Title,
            string Description,
            string AnnouncerInfo,
            string FileInfo,
            string TimeLength,
            string BookLinkPic,
            string BookLinkInfo
            )
        {
            int rowsAffected;

            SqlParameter[]
                parameters = {
								 new SqlParameter("@LBID",SqlDbType.Int),
								 new SqlParameter("@title",SqlDbType.VarChar,500), 
								 new SqlParameter("@Description",SqlDbType.VarChar,-1),
								 new SqlParameter("@AnnouncerInfo",SqlDbType.VarChar,5000),
								 new SqlParameter("@FileInfo",SqlDbType.VarChar,1000), 
								 new SqlParameter("@TimeLength",SqlDbType.VarChar,500), 
								 new SqlParameter("@BookLinkPic",SqlDbType.VarChar,1000),
								 new SqlParameter("@BookLinkInfo",SqlDbType.VarChar,5000)
			};

            parameters[0].Value = LBID;
            parameters[1].Value = Title;
            parameters[2].Value = Description;
            parameters[3].Value = AnnouncerInfo;
            parameters[4].Value = FileInfo;
            parameters[5].Value = TimeLength;
            parameters[6].Value = BookLinkPic;
            parameters[7].Value = BookLinkInfo;

            try
            {
                RunProcedure("LB_Modify_One_ListeningBook_Info", parameters, out rowsAffected);
            }
            catch (SqlException e)
            {
                throw new System.Exception(e.Message);
            }
            return (rowsAffected == 1);
        }

        //得到一个听书节目的相关内容
        public DataSet Get_One_ListeningBook_Info_From_LBID(string LBID)
        {
            SqlParameter[]
                parameters = {
								 new SqlParameter("@LBID", SqlDbType.Int)
							 };

            parameters[0].Value = LBID;

            using (DataSet ds = RunProcedure("LB_Search_ListeningBook_List", parameters, "OneListeningBookInfo"))
            {
                return ds;
            }
        }

        //一般用户或者管理员获取交流主题的标题列表
        public DataSet LB_Search_ListeningBook_List
            (
            string Title,
            string IsAll,

            int PageIndex,
            int PageSize,
            out int PageCount,
            out int TotalCount
            )
        {
            SqlParameter[]
                parameters = {
								 new SqlParameter("@Title", SqlDbType.VarChar,500),
								 new SqlParameter("@IsAll", SqlDbType.VarChar,4),

								 new SqlParameter("@pageindex", SqlDbType.Int),
								 new SqlParameter("@pagesize", SqlDbType.Int)
							 };

            parameters[0].Value = Title;
            parameters[1].Value = IsAll;        //指示存储过程获取主题信息(part or all)
            parameters[2].Value = PageIndex;
            parameters[3].Value = PageSize;

            using (DataSet ds = RunProcedure("LB_Search_ListeningBook_List", parameters, "ListeningBookList", out PageCount, out TotalCount))
            {
                return ds;
            }
        }

        //屏蔽或显示一条
        public bool LB_ShieldORDisp_One_ListeningBook_By_LBID
        (
            string LBID,
            out int ReturnValue,
            out string ReturnFieldValue
            )
        {
            int rowsAffected;

            SqlParameter[]
                parameters = {
								 new SqlParameter("@LBID",SqlDbType.Int)
			};

            parameters[0].Value = LBID;

            try
            {
                ReturnValue = RunProcedure("LB_ShieldORDisp_One_ListeningBook_By_LBID", parameters, out rowsAffected, out ReturnFieldValue);
            }
            catch (SqlException e)
            {
                throw new System.Exception(e.Message);
            }
            return (rowsAffected == 1);
        }


        //置顶一条
        public bool LB_StickORCannel_One_ListeningBook_By_LBID
        (
            string LBID,
            out int ReturnValue,
            out string ReturnFieldValue
            )
        {
            int rowsAffected;

            SqlParameter[]
                parameters = {
								 new SqlParameter("@LBID",SqlDbType.Int)
			};

            parameters[0].Value = LBID;

            try
            {
                ReturnValue = RunProcedure("LB_StickORCannel_One_ListeningBook_By_LBID", parameters, out rowsAffected, out ReturnFieldValue);
            }
            catch (SqlException e)
            {
                throw new System.Exception(e.Message);
            }
            return (rowsAffected == 1);
        }


    }
}
